package com.fs.base.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author TiidlerCJ
 *
 */
public class DateUtils {
	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	public static String formatDate(Date date, String pattern) {
    	if(pattern==null || pattern.equals("")){
    		pattern = DEFAULT_PATTERN; //"yyyy-MM-dd HH:mm:ss"
    	}
	     java.text.DateFormat df = new java.text.SimpleDateFormat(pattern); 
	     String s = df.format(date); 
	     return s;
	}
	
	public static String curDate(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }

    public static String curDate() {
        return curDate(DEFAULT_PATTERN);
    }
    
    
    public static String startTime() {
        return curDate("yyyy-MM-dd 00:00:00");
    }
    
    public static String endTime() {
    	return curDate("yyyy-MM-dd 23:59:59");
    }
    
    public static int differentDaysByMillisecond(Date start,Date end){
        int days = (int) ((end.getTime() - start.getTime()) / (1000*3600*24));
        return days;
    }
    
    public static Date parseDate(String dateStr, String pattern) {
    	if(pattern==null || pattern.equals("")){
    		pattern = DEFAULT_PATTERN; //"yyyy-MM-dd HH:mm:ss"
    	}
        SimpleDateFormat DATEFORMAT = new SimpleDateFormat(pattern);
        java.util.Date ret = null;
        try {
            ret = DATEFORMAT.parse(dateStr);
        } catch (Exception e) {
        	System.err.println(dateStr+"can't parseTo pattern Style!");
            e.printStackTrace();
        }
        return ret;
    }
}
